﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NuMvvm.Validation;
using NuMvvm.Messaging;
using System.Windows.Input;
using NuMvvm.Commands;

namespace NuMvvm.Tests
{
    public class PersonViewModel : ViewModelBase
    {
        private readonly IMessenger Messenger;

        private string _name;
        private string _email;

        private DelegateCommand _registerCommand;

        public PersonViewModel(IMessenger messenger)
        {
            Messenger = Messaging.Messenger.Default;
        }

        public string Name
        {
            get
            {
                return _name;
            }

            set
            {
                _name = value;
                OnPropertyChanged();
            }
        }

        [NotEmpty]
        [RegularExpression(RegularExpressions.Email, ErrorMessage ="Invalid email address.", AllowNullOrEmpty = true)]
        public string Email
        {
            get
            {
                return _email;
            }

            set
            {
                if (SetProperty(ref _email, value))
                {
                    ValidateProperty();

                    RegisterCommand.RaiseCanExecuteChanged();
                }
            }
        }

        public ICommand RegisterCommand
        {
            get
            {
                return _registerCommand ?? (_registerCommand = new DelegateCommand(() =>
                {
                    Messenger.Send(new TextMessage(string.Format("Registered {0}.", Name)));
                }, 
                () => {
                    return IsValid;
                }));
            }
        }
    }
}
